package com.huikeportal.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huikeportal.controller.common.Lang;
import com.huikeportal.model.AllIndexModel;
import com.huikeportal.model.ChangeGcParam;
import com.huikeportal.model.Language;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteCommon;
import com.huikeportal.model.SquareChange;
import com.huikeportal.model.TodayIndexes;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.UserSiteHistory;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;
import com.huikeportal.service.TodayIndexesService;

@Controller
@Scope("prototype")
public class FootfallWholeController {
	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;
	
	@Autowired
	private TodayIndexesService todayIndexesService;
	
	@Autowired
	private FootfallGlobalService footfallGlobalService;

	@Autowired
	SelectSiteService selectSiteService;
	@RequestMapping("/footfallWhole")
	public String totalTraffic(ModelMap model,String siteId) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo=loginService.getUserInfo(userInfo);
		String userId=userInfo.getUserId();
		 //获取当前系统语言
        String sysLang = session.getAttribute("sysLang").toString();
        //查询当前页面id
        SqlParameter sql = new SqlParameter();
        sql.setSysLang(sysLang);
        sql.setString("footfallWhole");
        Integer menuId = selectSiteService.getMenuId(sql);
        //获取用户权限按钮信息
        List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
                userId, menuId);
        String buttonExport = "0";
        for(WordBook wordBook:buttonList){
        	if("导出".equals(wordBook.getDicValue())){
        		buttonExport = "1";
        		break;
        	}
        }
        /**初始化全局分析的指标权限*/
        /**获取用户指标权限*/
        List<String> userIndexList = footfallGlobalService.getUserIndex(userInfo);
        List<String> siteTypes = new ArrayList<String>();
        siteTypes.add(FootfallConstant.WHOLE_SITETYPE);
        //我的数据_实时指标 model为1_1
        String moduleId1 = FootfallConstant.MYFOOTFALL_CUR;
        //我的数据_趋势分析 model为1_2
        String moduleId2 = FootfallConstant.MYFOOTFALL_TREND;
      //我的数据_指标明细 model为1_3
        String moduleId3 = FootfallConstant.MYFOOTFALL_DETIL;
        //我的数据-指标排行 model为1_4
        String moduleId4 = FootfallConstant.MYFOOTFALL_RANK;
        
        //获取实时指标	
        List<String> indexListCurrent = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId1, siteTypes);
        model.addAttribute("indexListCurrent ",indexListCurrent);
      //获取趋势分析指标详解
        List<String> indexListDetil = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId3, siteTypes);
        model.addAttribute("indexListDetil",indexListDetil);
        
        
        //获取趋势分析指标
        List<String> indexList = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId2, siteTypes);
        //取趋势分析指标分组
        HashMap<String, List<String>> indexListOfGroup = footfallGlobalService.getIndexListOfGroup(indexList);
        model.addAttribute("indexList",JSONObject.fromObject(indexListOfGroup));
        
        //获取排行榜各场所指标
        HashMap<String, Object> siteIndexMap = new HashMap<String, Object>();
        List<String> siteTypes200 = new ArrayList<String>();
        siteTypes200.add(FootfallConstant.DIQU_SITETYPE);
        List<String> index200 = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId4, siteTypes200);
        HashMap<String, List<String>> index200OfGroup = footfallGlobalService.getIndexListOfGroup(index200);
        siteIndexMap.put("index200",index200OfGroup);
        List<String> siteTypes300 = new ArrayList<String>();
        siteTypes300.add(FootfallConstant.WHOLE_SITETYPE);
        List<String> index300 = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId4, siteTypes300);
        HashMap<String, List<String>> index300OfGroup = footfallGlobalService.getIndexListOfGroup(index300);
        siteIndexMap.put("index300",index300OfGroup);
        List<String> siteTypes400 = new ArrayList<String>();
        siteTypes400.add(FootfallConstant.AREA_SITETYPE);
        List<String> index400 = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId4, siteTypes400);
        HashMap<String, List<String>> index400OfGroup = footfallGlobalService.getIndexListOfGroup(index400);
        siteIndexMap.put("index400",index400OfGroup);
        List<String> siteTypes500 = new ArrayList<String>();
        siteTypes500.add(FootfallConstant.FLOOR_SITETYPE);
        List<String> index500 = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId4, siteTypes500);
        HashMap<String, List<String>> index500OfGroup = footfallGlobalService.getIndexListOfGroup(index500);
        siteIndexMap.put("index500",index500OfGroup);
        List<String> siteTypes600 = new ArrayList<String>();
        siteTypes600.add(FootfallConstant.STORE_SITETYPE);
        List<String> index600 = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId4, siteTypes600);
        HashMap<String, List<String>> index600OfGroup = footfallGlobalService.getIndexListOfGroup(index600);
        siteIndexMap.put("index600",index600OfGroup);
        List<String> siteTypes700 = new ArrayList<String>();
        siteTypes700.add(FootfallConstant.CHANNEL_SITETYPE);
        List<String> index700 = footfallGlobalService.getIndexListOfModel(userIndexList, moduleId4, siteTypes700);
        HashMap<String, List<String>> index700OfGroup = footfallGlobalService.getIndexListOfGroup(index700);
        siteIndexMap.put("index700",index700OfGroup);
        
        model.addAttribute("siteIndexMap",JSONObject.fromObject(siteIndexMap));
        
        //获取排行榜各时间指标
        HashMap<String, Object> dateIndexMap = new HashMap<String, Object>();
        List<String> indexHour  = footfallGlobalService.getTimeIndex("hour");
        dateIndexMap.put("indexHour", indexHour);
        List<String> indexDay  = footfallGlobalService.getTimeIndex("day");
        dateIndexMap.put("indexDay", indexDay);
        List<String> indexWeek  = footfallGlobalService.getTimeIndex("week");
        dateIndexMap.put("indexWeek", indexWeek);
        List<String> indexMonth  = footfallGlobalService.getTimeIndex("month");
        dateIndexMap.put("indexMonth", indexMonth);
        List<String> indexYear  = footfallGlobalService.getTimeIndex("year");
        dateIndexMap.put("indexYear", indexYear);
        model.addAttribute("dateIndexMap",JSONObject.fromObject(dateIndexMap));

        HashMap<String, String>  indexGroupInfo = footfallGlobalService.getIndexGroupInfo();
        model.addAttribute("indexGroupInfo",JSONObject.fromObject(indexGroupInfo));
        HashMap<String, String>  indexInfo = footfallGlobalService.getIndexInfo();
        model.addAttribute("indexInfo",JSONObject.fromObject(indexInfo));
        /**初始化全局分析的指标权限结束*/
        /**
         * 初始化场所
         */
		String siteType=FootfallConstant.WHOLE_SITETYPE;//场所类型是300
		String siteID="";
		SiteAllInfo siteAllInfo=footfallGlobalService.selectSiteByUserId(userInfo,siteType);
		if(siteAllInfo == null||siteAllInfo.getSiteid()==null||"".equals(siteAllInfo.getSiteid())){
			String menuHtml = loginService.getMenuHtml(userInfo, "footfallWhole");
			model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
			return "noPermission";	
		}
		
		
//		String menuId=FootfallConstant.WHOLE_MENUID;
//		String sessionId=userId+menuId;
		String sessionId= "userSiteSession";
		String sessionSiteId ="";
//		UserSiteHistory userSiteHistory=new UserSiteHistory();
		if(session.getAttribute(sessionId)!=null){
//			userSiteHistory=(UserSiteHistory) session.getAttribute(sessionId);
			sessionSiteId = session.getAttribute(sessionId).toString();
		}
		//从seesion中取siteid
		//首先查找session找，然后是页面传过来，再是根据用户取
		UserSiteHistory userSiteHistory1=new UserSiteHistory();
		if(siteId!=null&& !siteId.equals("")){
			siteID=siteId;
			userSiteHistory1.setSiteId(siteID);
			userSiteHistory1.setUserId(userId);
			userSiteHistory1.setMenuId(menuId);
			footfallGlobalService.updateUserSiteHistory(userSiteHistory1);//修改数据库记录
			session.setAttribute(sessionId, siteID);//如果没有将新得到的siteId存入session
		}else if(sessionSiteId!=null&&!"".equals(sessionSiteId)){
			siteID=sessionSiteId;
//			userSiteHistory1.setSiteId(siteID);
//			userSiteHistory1.setUserId(userId);
//			userSiteHistory1.setMenuId(menuId);
//			footfallGlobalService.updateUserSiteHistory(userSiteHistory1);//修改数据库记录
		}else{
			UserSiteHistory param = new UserSiteHistory(); 
			param.setMenuId(menuId);
			param.setUserId(userId);
			UserSiteHistory userSiteHistoryda=footfallGlobalService.selectUserSiteHistory(param);
			if(userSiteHistoryda!=null&&!"".equals(userSiteHistoryda.getSiteId())){
				siteID = userSiteHistoryda.getSiteId();
				session.setAttribute(sessionId, siteID);//如果没有将新得到的siteId存入session
			}else{
				siteID=siteAllInfo.getSiteid();
				userSiteHistory1.setSiteId(siteID);
				userSiteHistory1.setUserId(userId);
				userSiteHistory1.setMenuId(menuId);
				footfallGlobalService.updateUserSiteHistory(userSiteHistory1);//修改数据库记录
				session.setAttribute(sessionId, siteID);//如果没有将新得到的siteId存入session
				
			}
		}
		
		// 获取用户根据店铺反查的广场权限
		List<SiteAllInfo> dpGcList = loginService.getDpGcSiteList(userInfo);
		
		// 初始化默认广场信息
		SiteCommon siteCommon = selectSiteService.querySiteInfo(siteID);
		//如果记录的siteId已经变更了，则取默认用户权限下的siteId
		if(siteCommon == null){
			siteID = siteAllInfo.getSiteid();
			siteCommon = selectSiteService.querySiteInfo(siteAllInfo.getSiteid());
		}
		
		model.addAttribute("siteCommon", siteCommon);
		// 更换广场div初始化数据
		ChangeGcParam changeGcParam = new ChangeGcParam();
		changeGcParam.setSelectType("0");// 单选广场
		changeGcParam.setGcString(siteCommon.getSiteId());// 已经选择的广场id
		List<SiteAllInfo> yuanGcList = loginService.getGcSiteList(userInfo);
		SquareChange squareChange = selectSiteService.squareChangeLoad(
				changeGcParam, dpGcList,yuanGcList);// 获取初始化更换广场信息
		model.addAttribute("squareChange", squareChange);
		model.addAttribute("selectType", changeGcParam.getSelectType());
		model.addAttribute("backPage", "footfallWhole");
		
		if(siteID!=null&& !siteID.equals("")){
			String siteNames=footfallGlobalService.getSiteNames(siteID);
//			String siteName=todayIndexesService.getSiteNameBySiteId(siteID);
			model.addAttribute("siteName", siteNames);
			model.addAttribute("siteId", siteID);
			model.addAttribute("datetime", footfallGlobalService.getDate());
			HashMap<String, Object> todayIndexes= todayIndexesService.getTodayIndexes(siteID,indexListCurrent);
			model.addAttribute("todayIndexes",todayIndexes);
		}
		model.addAttribute("buttonExport",buttonExport);
		Language lang = Lang.getInfo(sysLang);
		model.addAttribute("lang",lang);
		String menuHtml = loginService.getMenuHtml(userInfo, "footfallWhole");
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		return "footfallWhole";
	}
}
